home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n2.arc / BSOUND.ASM < prev    next >
Assembly Source File  |  1990-06-14  |  2KB  |  78 lines

  1. ;** BSOUND.ASM - simulates BASIC's SOUND statement for any language
  2. ;
  3. ;Copyright (c) 1990 Ethan Winer
  4. ;
  5. ;Syntax: CALL BSound(Frequency%, Duration%)
  6. ;
  7. ;Where Frequency% is the frequency in Hertz (cycles per second) and
  8. ;Duration% is the number of 1/18ths second to sustain the tone.
  9. ;These parameters are identical to those for the SOUND statement in
  10. ;the BASIC reference manual, and the range of values for the pitch
  11. ;is the same: 37 Hz to 32767 Hz.
  12. ;
  13. ;This file is intended to be assembled with MASM 5.1 or later.
  14.  
  15.  
  16. .Model Medium, Basic
  17. .Code
  18.  
  19. BSound Proc, Frequency:Word, Duration:Word
  20.  
  21.     Mov  AL,10110110b     ;initialize Timer 2
  22.     Out  43h,AL
  23.  
  24.     Mov  DX,12h           ;load DX:AX with 1,190,000 to convert
  25.     Mov  AX,2870h         ;  the incoming frequency to period (1/f)
  26.     Mov  BX,Frequency     ;get the address for Frequency%
  27.     Mov  BX,[BX]          ;put Frequency% into BX
  28.     Cmp  BX,37            ;is it less than 37 Hz.?
  29.     Jb   Exit             ;yes, avoid "Divide by zero" overflow err.
  30.     Div  BX               ;now AX holds the correct timer
  31.                           ; interval period
  32.  
  33.     Out  42h,AL           ;send the interval information to Timer 2
  34.     Mov  AL,AH            ;get second byte
  35.                           ; (a word must be sent as two bytes)
  36.     Out  42h,AL           ;send it
  37.  
  38.     In   AL,61h           ;read timer port "B"
  39.     Or   AL,00000011b     ;set the bits to turn the speaker on
  40.     Out  61h,AL           ;turn it on
  41.  
  42.     Push Duration         ;show Pause where Duration% is
  43.     Call Far Ptr Pause    ;delay for Duration% clock ticks
  44.  
  45.     In   AL,61h           ;read timer port "B"
  46.     And  AL,11111100b     ;set the bits to turn the speaker off
  47.     Out  61h,AL           ;turn it off
  48.  
  49. Exit:
  50.     Ret                   ;return to caller
  51.  
  52. BSound Endp
  53.  
  54.  
  55.  
  56. Pause Proc, Ticks:Word
  57.  
  58.     Mov  BX,Ticks         ;get address for Ticks%
  59.     Mov  CX,[BX]          ;put it in CX
  60.     Jcxz Exit             ;zero is a mistake, avoid a LONG delay
  61.  
  62.     Xor  AX,AX            ;look at the low system timer byte
  63.     Mov  ES,AX            ;through ES
  64.  
  65. Outer:
  66.     Mov  AL,ES:[46Ch]     ;get the current timer count
  67.  
  68. Inner:
  69.     Cmp  AL,ES:[46Ch]     ;has it changed?
  70.     Je   Inner            ;no, keep checking
  71.     Loop Outer            ;yes, loop until we've exhausted Ticks%
  72.  
  73. Exit:
  74.     Ret                   ;return to caller
  75.  
  76. Pause Endp
  77. End
  78.